package com.yanqu.road.server.cmd.activity.busyfarming;

import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingModule;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

@Cmd(code = Protocol.S_BUSY_FARMING_STEAL,desc = "偷菜")
public class BusyFarmingStealCmd extends AbstractCommand {

    int landId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BusyFarmingProto.BusyFarmingStealReqMsg reqMsg = BusyFarmingProto.BusyFarmingStealReqMsg.parseFrom(packet.getBytes());
        landId = reqMsg.getLandId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(BusyFarmingMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!BusyFarmingMgr.activityInTime()){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NO_IN_TIME;
            return;
        }
        if(landId < 0 || landId > BusyFarmingMgr.ALL_LAND_NUM - 1){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        //是否开启；需活动进行多久后开启
        int needHour = BusyFarmingMgr.getBusyFarmingOptOpenTimeParam().get(0);
        int needTIme = needHour * 3600;
        if(DateHelper.getCurrentSecond() - BusyFarmingMgr.getActivityInfo().getBeginTime() < needTIme){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_STEAL_TIME_NOT_OPEN;
            return;
        }

        if(BusyFarmingMgr.isRestTime()) {
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NOT_IN_OPT_TIME;
            return;
        }

        ret = player.getModule(BusyFarmingModule.class).steal(landId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BusyFarmingProto.BusyFarmingStealRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingStealRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setLandId(landId);
            player.sendPacket(Protocol.U_BUSY_FARMING_STEAL, respMsg);
        }
    }
}
